/*
 * author      : 吴中勤 
 * date        : 2019/3/19
 * description : TODO
 */

package org.wzq.learn;

import java.util.Random;

/*
 * https://www.geeksforgeeks.org/java-lang-inheritablethreadlocal-class-examples/
 *
 */
public class TestThreadLocal {
    static ThreadLocal<Integer> intValue = ThreadLocal.withInitial(() -> {
        System.out.printf("init name=%s\n", Thread.currentThread().getName());
        Random rdm = new Random();
        return rdm.nextInt(100);
    });

    // mac fn + shift + F10 执行
    public static void main(String[] args) {
        /*变量只有一个 testThreadLocal, 但是在不同对线程中, 其 intValue 是不同的 */
        // TestThreadLocal testThreadLocal = new TestThreadLocal();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                System.out.printf("name=%s, intValue=%d\n", Thread.currentThread().getName(), TestThreadLocal.intValue.get());
            }).start();
        }
    }
}
